package email_autochecker;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

public class ClickButtonOk implements ActionListener{

	private Account account;
	private String regExpr;
	private String user;
	private String domain;
	private String[] part;
	public ClickButtonOk(Account acc) {
		account = acc;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// Create methods for OK button
		if(!verifyFields(account.getTextEmail().getText(), account.getTextPass().getPassword().toString())){
			//account.getTextEmail().setBorder(BorderFactory.createLineBorder(Color.RED));
			account.getTextEmail().setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED), "Email"));
		}else{
			account.getTextEmail().setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Email"));
			JOptionPane.showMessageDialog(null, "Login efectuado com sucesso");
			
			account.setVisible(false);
			
		}
	
	}

	public boolean verifyFields(String mail, String pass) {
		regExpr = "[a-zA-Z_0-9]";
		System.out.println(mail);
		if (mail.contains("@")) {
			part = mail.split("@");
			user = part[0];
			domain = part[1];
			System.out.println(domain);
			user.matches(regExpr);
			System.out.println(!domain.equals("gmail.com"));
			
			if ( !(domain.equals("isec.pt")) && !(domain.equals("gmail.com"))
					&& !(domain.equals("hotmail.com")) ) {
				return false;
			}

			return true;
		}
		return false;
	}
}
